package com.etc.controller;
import com.etc.entity.Staff;
import com.etc.service.StaffService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.servlet.ModelAndView;


import java.io.PrintWriter;
import java.util.List;

@Controller
@RequestMapping("staff")
public class StaffController {

    private String index_url = "index";

    @Autowired
    private StaffService staffService;

    @RequestMapping("index")
    public ModelAndView index(){
        ModelAndView modelAndView = new ModelAndView(index_url);
        List<Staff> byAll = staffService.findAll();
        modelAndView.addObject("staff",byAll);
        return modelAndView;
    }


    @RequestMapping("deleteId")
    public void deleteId(@RequestParam(value = "id") int id, PrintWriter out){
        Integer deleteId =staffService.deleteId(id);
        if (deleteId == 1){
            out.print(1);
        }else{
            out.print(2);
        }
    }



    @RequestMapping(value = "add", method = RequestMethod.POST)
    public void add(@RequestParam(value = "lastname") String lastname,
                    @RequestParam(value = "email") String email,
                    @RequestParam(value = "department") String department,
                    @RequestParam(value = "edit") String edit,
                    @RequestParam(value = "dalete") String dalete,
                    @RequestParam(value = "gender") String gender, PrintWriter out) {
        Integer add = staffService.add(new Staff(lastname, email, department, edit, dalete, gender));
        if (add == 1) {
            out.print(1);
        } else {
            out.print(2);
        }
    }
   @RequestMapping("findid")
    public void findid(@RequestParam(value = "id")int id,PrintWriter out) {
       Staff findid = staffService.findid(id);
       Gson gson = new Gson();
       //json   {"id":"3","username":"long","password":"dfdf"}
       String s = gson.toJson(findid);
       if (findid != null) {
           out.print(s);
       }
   }
       @RequestMapping("updateid")
        public void updateid(
           @RequestParam(value = "id") int id,
           @RequestParam(value = "lastname") String lastname,
           @RequestParam(value = "email") String email,
           @RequestParam(value = "department") String department,
           @RequestParam(value = "edit") String edit,
           @RequestParam(value = "dalete") String dalete,
           @RequestParam(value = "gender") String gender,PrintWriter out){
           Integer updateid = staffService.updateid(new Staff(id, lastname, email, department, edit, dalete, gender));
           if (updateid ==1){
          out.print(1);
      }else{
          out.print(2);
      }

       }
   }




